var limsCoreCode = Gikam.getPageObject({

    getCodeCategoryListColumns: function () {
        return [{
            checkbox: true
        }, {
            index: true
        }, {
            field: 'id',
            title: 'T_CORE_CODE_CATEGORY.ID',
            validators: ['notEmpty'],
            genericQuery: true,
            type: 'link',
            width: 200
        }, {
            field: 'codeCategoryName',
            validators: ['notEmpty'],
            title: 'T_CORE_CODE_CATEGORY.CODECATEGORYNAME',
            width: 200
        }, {
            field: 'ext$.codeclassname',
            title: 'T_CORE_CODE_CATEGORY.CODECLASSNAME',
        }];
    },

    getCodeListColumns: function () {
        return [{
            checkbox: true
        }, {
            index: true
        }, {
            title: 'T_CORE_CODE.CODE',
            field: 'code',
            type: 'link',
            validators: ['notEmpty'],
            genericQuery: true,
            width: 150
        }, {
            title: 'T_CORE_CODE.CODENAME',
            field: 'codeName',
            genericQuery: true,
            editor: true,
            validators: ['notEmpty', 'strLength[0,85]'],
            width: 150
        }, {
            title: 'T_CORE_CODE.I18NCODE',
            field: 'i18nCode',
            genericQuery: true,
            editor: true,
            width: 150
        }]
    },

    baseUrl: IFM_CONTEXT + '/secure/core/module/sys/lims-codes'
});

limsCoreCode.editPage = {

    getCodeCategoryGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-code-category-edit-list-grid',
            filterOpen: true,
            url: codeCategory.baseUrl + '/queries/raw',
            columns: limsCoreCode.getCodeCategoryListColumns(),
            page: true,
            order: {createdTime: "desc"},
            deleteFormatter: function (row) {
                return row.id;
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData(['core-code-edit-list-grid']);
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('core-code-edit-list-grid').done(function (grid) {
                    grid.refresh({
                        url: coreCode.baseUrl + '/queries',
                        requestData: {
                            codeCategoryId_EQ: row.id
                        }
                    });
                });
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'CORE.MODULE.SYS.CODECATEGORIES.TAB.ADD',
                        titleWidth: 100,
                        fields: [{
                            field: 'id',
                            title: 'T_CORE_CODE_CATEGORY.ID',
                            validators: ['remote[/secure/core/module/sys/code-categories/{id}/action/validate-unique]', 'notEmpty', 'strLength[0,33]'],
                            preInsert: true
                        }, {
                            field: 'codeCategoryName',
                            title: 'T_CORE_CODE_CATEGORY.CODECATEGORYNAME',
                            validators: ['notEmpty', 'strLength[0,21]'],
                            preInsert: true
                        }],
                        param: {
                            codeClassId: 'biz'
                        },
                        url: IFM_CONTEXT + '/secure/core/module/sys/lims-codes/add/category'
                    }).done(function (id) {
                        Gikam.getComp('lims-code-category-edit-list-grid').refresh();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove',
                onClick: function () {
                    let limsDelUrl = IFM_CONTEXT + '/secure/core/module/sys/lims-codes' + '/delete/category'
                    Gikam.getComp('lims-code-category-edit-list-grid').deleteRows(limsDelUrl);
                }
            }]
        }
    },
    /*
     * 字典类型表
     */
    getCoreCodeGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            page: true,
            id: 'core-code-edit-list-grid',
            columns: limsCoreCode.getCodeListColumns(),
            service: 'coreCodeService',
            deleteFormatter: function (row) {
                return row.codeName || row.code || row.id;
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    let activedRow = Gikam.getComp('lims-code-category-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.preInsert({
                        modalTitle: 'CORE.MODULE.SYS.CODES.TAB.ADD',
                        fields: [{
                            title: 'T_CORE_CODE.CODE',
                            field: 'code',
                            validators: ['strLength[0,32]'],
                            preInsert: true
                        }, {
                            title: 'T_CORE_CODE.CODENAME',
                            field: 'codeName',
                            validators: ['strLength[0,85]'],
                            preInsert: true
                        }],
                        url: limsCoreCode.baseUrl,
                        param: {
                            codeCategoryId: activedRow.id
                        },
                    }).done(function (id) {
                        Gikam.getComp('core-code-edit-list-grid').refresh();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove',
                onClick: function () {
                    var grid = Gikam.getComp('core-code-edit-list-grid');
                    var selections = grid.getSelections();
                    var validateFlag = true;
                    Gikam.each(selections, function (index, row) {
                        if (row.ext$.codeclassid === 'sys') {
                            validateFlag = false;
                            Gikam.alert('CORE.MODULE.SYS.CODES.TIP.CANNOT_DELETE_SYS_CODE');
                            return false;
                        }
                    });
                    if (validateFlag) {
                        grid.deleteRows(coreCode.baseUrl);
                    }
                }
            }]
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-core-code-list-layout',
            renderTo: workspace.window.$dom,
            west: {
                width: '50%',
                items: [this.getCodeCategoryGridParam()]
            },
            center: {
                items: [this.getCoreCodeGridParam()]
            }

        });
    },

    init: function (param) {
        this.param = param;
        this.create();
    }
};
